package ar.edu.unsam.algo3.plantasvszombies.web

import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila
import ar.edu.unsam.algo3.plantasvszombies.dominio.Zombie
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.AlmanaqueAppModel
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinAppModel
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.JardinZenAppModel
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.BatallaAppModel
import org.uqbar.commons.model.UserException

class PlantasVsZombiesController {
	static scope = "session"

	static allowedMethods = [
		sumar: 'POST'
	]

	def JardinAppModel tablero
	def JardinZenAppModel jardinZen
	def AlmanaqueAppModel almanaqueZombies
	def BatallaAppModel batallaAM = new BatallaAppModel()
	def Fila fila = new Fila()
	def zombies


	PlantasVsZombiesController(){
		tablero = new JardinAppModel()
		jardinZen = new JardinZenAppModel()
		almanaqueZombies = new AlmanaqueAppModel()
	}

	def index() {
		[jardin:tablero.jardin]
	}

	def eliminarFila(String id){
		def ids = new Integer(id)
		tablero.misFilas.remove(ids)
		redirect(action: "index")
	}

	def agregarFila(String id){
		def message = ""
		try{
			def Fila fila = new Fila()
			def ids = new Integer(id)
			fila.jardinZen = tablero.jardin.tiposPosibles.get(ids)
			tablero.jardin.agregarFila(fila)
			redirect (action: "index")
		} catch(UserException e){
			render(view:"index", model: ['jardin': tablero.jardin, 'message':  e.message])
		}
	}

	def fila(){
		fila = tablero.jardin.filas.get(new Integer(params.idFila))
		[fila:fila, idFila:params.idFila]
	}

	def agregarPlanta(String id){
		try{
			def ids = new Integer(id)
			def planta = fila.jardinZen.plantines.get(ids)
			fila.agregarPlanta(planta)
			render(view:"fila")
		}catch(UserException e){
			render{
				div(id: "error", e.message)
			}
		}
	}

	def eliminarPlanta(){
		def ids = new Integer(params.planta)
		def mifila = tablero.jardin.filas.get(new Integer(params.idFila))
		mifila.plantas.remove(ids)
		tablero.actualizar()
		redirect(action:"fila", params:[idFila: params.idFila])
	}

	def jardinZen(String id){
		def ids = new Integer(id)
		jardinZen.jardin = tablero.jardin
		jardinZen.jardinZen = tablero.jardin.tiposPosibles.get(ids)
		jardinZen.actualizar()
		if (ids == 0){
			params.id = 1
		}else{
			params.id = 0
		}
		[jardinZen:jardinZen, jardin:tablero]
	}

	def almanaque(){
		almanaqueZombies.nombre=params.nombre
		almanaqueZombies.buscar()
		[almanaqueZombies:almanaqueZombies]
	}

	def iniciarBatalla(){
		this.batallaAM = new BatallaAppModel()
		redirect(action:"batalla", params: [idFila: params.idFila])
	}


	def batalla() {
		batallaAM.fila = tablero.jardin.filas.get(new Integer(params.idFila))
		almanaqueZombies = new AlmanaqueAppModel()
		almanaqueZombies.buscar()
		[batalla:batallaAM, almanaqueZombies:almanaqueZombies, numFila: params.idFila]
	}

	def atacar(){
		this.batallaAM = new BatallaAppModel()
		def numZombie = new Integer(params.idZombie)
		batallaAM.batalla.zombie = almanaqueZombies.result.get(numZombie)
		batallaAM.batalla.fila = tablero.jardin.filas.get(new Integer(params.idFila))
		batallaAM.batalla.atacar()
		redirect(action:"batalla", params: [idFila: params.idFila])
	}
}
